package com.stdspringboot.ctrl;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageInfo;
import com.stdspringboot.entity.Goods;
import com.stdspringboot.entity.Leave;
import com.stdspringboot.entity.User;
import com.stdspringboot.service.IGoodsService;
import com.stdspringboot.util.Constant;
import com.stdspringboot.util.DateUtil;
import com.stdspringboot.util.JWTUtil;
import com.stdspringboot.util.UUIDUtil;
import com.stdspringboot.vo.GoodsVO;
import com.stdspringboot.vo.JsonResult;
import com.stdspringboot.vo.UserInfoJWT;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/goods")
public class GoodsCtrl {

    @Resource
    private IGoodsService service;

    @RequestMapping("/query")
    public JsonResult query(GoodsVO goodsVO, Integer pageNum, Integer pageSize){
        if(pageNum==null){
            pageNum=1; //当前页数
        }
        if(pageSize==null){
            pageSize=10;//意思是：每页显示10条记录
        }
        JsonResult jr=null;
        PageInfo page=service.query(goodsVO,pageNum,pageSize);

        jr=JsonResult.success("查询成功！",page);
        return jr;
    }

    @RequestMapping("/deleteById")
    public JsonResult deleteById(String id){

        //将字符串转换成Integer类型
        Integer idInt=Integer.parseInt(id);
        int i=service.deleteById(idInt);

        JsonResult jr=null;
        if(i>0){
            jr=JsonResult.success("删除成功！");
        }else{
            jr=JsonResult.fail(JsonResult.CODE_RECORD_NO_EXIST,"记录不存在，无法删除！");
        }

        return jr;
    }


    @RequestMapping("/add")
    public JsonResult add(Goods goods, @RequestParam("imgUrlList") List imgUrlList, HttpServletRequest request) throws IOException {

        //从header获取jwt，并解析成 UserInfoJWT对象
        String token=request.getHeader("X-Token");
        UserInfoJWT uij=JWTUtil.parse(token, UserInfoJWT.class);

        String userId=uij.getId();

        goods.setUserId(  userId  );


        JsonResult jr=null;
        int i=service.add(goods,imgUrlList);
        if(i>0){
            jr=JsonResult.success("添加成功！");
        }else{
            jr=JsonResult.fail(JsonResult.CODE_ADD_FAIL,"添加失败！");
        }

        return jr;
    }

    @RequestMapping("/showById")
    public JsonResult showById(Integer id){

        GoodsVO goods=service.showById(id);
        JsonResult jr=null;
        if(goods==null){
            jr=JsonResult.fail(JsonResult.CODE_RECORD_NO_EXIST,"该条记录不存在！");
        }else{
            jr=JsonResult.success(null,goods);
        }
        return jr;

    }

    @RequestMapping("/updateById")
    public JsonResult modifyById(Goods goods){
        int i=service.modifyById(goods);

        JsonResult jr=null;
        if(i==0){
            jr=JsonResult.fail(JsonResult.CODE_RECORD_NO_EXIST,"该条记录不存在！");
        }else{
            jr=JsonResult.success("修改成功！");
        }

        return jr;
    }

    /**
     *
     * @param img 要与前端上传文件控件同名
     * @return
     */
    @RequestMapping("/upload")
    public JsonResult upload(MultipartFile img) throws IOException {
        //防止不同用户上传的文件名相同，导致文件互相覆盖，所以用uuid作为文件名，如：将1.jpg改为2384234.jpg
        //获取上传文件的名字
        String oFilename=img.getOriginalFilename();
        //截取文件后缀，如：将 1.jpg 截取出 .jpg
        int pos=oFilename.lastIndexOf(".");
        String suffix=oFilename.substring(pos);

        String uuid=UUIDUtil.uuid();
        String newFile=uuid+suffix;

        /*
        拼装保存上传文件的路径
         */
        //找到classpath
        File classpath=ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX);
        //拼装保存上传文件的路径
        File uploadFile=new File(classpath,"/static/upload");
        //判断该文件夹是否存在，不存在就创建
        if(!uploadFile.exists()){
            uploadFile.mkdirs();
        }

        /*
        将上传的文件，移动到保存目录下
         */
        //拼装文件的全路径，包含路径+文件名
        File file=new File(uploadFile,newFile);
        img.transferTo(file);

        //将访问图片的url返回给前端
        return JsonResult.success("上传成功！","upload/"+newFile);
    }
}
